<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="编码原则" prop="bmBmyz">
        <el-input
          v-model="queryParams.bmBmyz"
          placeholder="请输入编码原则"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['project:bm:export']"
        >
          导出
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="bmList"
      border
      stripe
      highlight-current-row
      :row-style="{ padding: '0px' }"
      :cell-style="{ padding: '0px 0' }"
      :header-row-style="{ padding: '2px 0' }"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编码原则" align="center" prop="bmBmyz" />
      <el-table-column label="仓库名称" align="center" prop="storageName" />
      <el-table-column label="创建人员" align="center" prop="createBy" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改类别代码-物料编码匹配规则对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编码原则" prop="bmBmyz">
          <el-input v-model="form.bmBmyz" placeholder="请输入编码原则" />
        </el-form-item>
        <el-form-item label="仓位ID" prop="bmStorageId">
          <el-input
            v-model="form.bmStorageId"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="仓位名称" prop="bmStorageId">
          <el-select v-model="form.bmStorageId" placeholder="请选择仓位代码">
            <el-option
              v-for="dict in storageOptions"
              :key="dict.storageId"
              :label="dict.storageName"
              :value="dict.storageId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBm,
  getBm,
  delBm,
  addBm,
  updateBm,
  exportBm,
} from "@/api/project/bm";
import { listStorage } from "@/api/repertory/Storage";

export default {
  name: "Bm",
  data() {
    return {
      //仓位信息
      storageOptions: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 类别代码-物料编码匹配规则表格数据
      bmList: [],
      // 弹出层标题
      title: "",
      //计算渲染无效值
      iCur: null,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bmBmyz: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bmBmyz: [
          { required: true, message: "编码原则不能为空", trigger: "blur" },
        ],
        bmStorageId: [
          { required: true, message: "仓位ID不能为空", trigger: "blur" },
        ],
        createBy: [
          { required: true, message: "创建人员不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询类别代码-物料编码匹配规则列表 */
    getList() {
      this.loading = true;
      this.tableH = document.body.clientHeight - 280 > 600 ? 657 : 400;
      this.queryParams.pageSize = this.tableH > 600 ? 25 : 15;
      listBm(this.queryParams).then((response) => {
        this.bmList = response.rows;
        this.total = response.total;
        this.icur = document.body.clientHeight;

        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bmId: null,
        bmBmyz: null,
        bmStorageId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.bmId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      listStorage().then((response) => {
        //需要请求所有仓位
        this.storageOptions = response.rows;

        this.open = true;
        this.title = "添加类别代码-物料编码匹配规则";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bmId = row.bmId || this.ids;
      getBm(bmId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改类别代码-物料编码匹配规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.bmId != null) {
            updateBm(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBm(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bmIds = row.bmId || this.ids;
      this.$confirm(
        '是否确认删除类别代码-物料编码匹配规则编号为"' + bmIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delBm(bmIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm(
        "是否确认导出所有类别代码-物料编码匹配规则数据项?",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.exportLoading = true;
          return exportBm(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
